package kr.saeildamil.androidforstudy2.ch33;

import kr.saeildamil.androidforstudy2.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;

public class MPTest extends Activity {

	MediaPlayer mPlayer;
	String mSDpath;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch33_mptest);

		mPlayer = MediaPlayer.create(this, R.raw.dingdong);
		mSDpath = Environment.getExternalStorageDirectory().getAbsolutePath();

	}
	
	@Override
	protected void onPause() {
		super.onPause();
		if(mPlayer != null){
			mPlayer.release();
			mPlayer = null;
		}
	}
	
	public void mOnClick(View v) {
		MediaPlayer player;

		switch (v.getId()) {
		case R.id.btn1: // resource 재생;
			player = MediaPlayer.create(this, R.raw.dingdong);
			player.start();
			break;
		case R.id.btn2: // file 재생;
			player = new MediaPlayer();
			try {
				player.setDataSource(mSDpath + "/themeoftrixie.mp3");
				player.prepare();
				player.start();
			}catch (Exception e) {
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.btn3:		// Stream 재생;
			player = new MediaPlayer();
			Uri uri = Uri.parse("http://www.soen.kr/data/saemaul1.mp3");
			try {
				player.setDataSource(this, uri);
				player.prepare();
			} catch (Exception e) {
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
			}
			player.start();
			break;
		case R.id.btn4:		// 미리 준비된 객채로 생성, 재생;
			mPlayer.seekTo(0);
			mPlayer.start();
			break;
		case R.id.btn5:		// 준비되지 않은(prepare()가 없는) 재생;
			player = new MediaPlayer();
			try {
				player.setDataSource(mSDpath + "/themeoftrixie.mp3");
			//  player.prepare();
				player.start();
			} catch (Exception e) {
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.btn6:
			player = MediaPlayer.create(this, R.raw.dingdong);
			try {
				player.prepare();
				player.start();
			} catch (Exception e) {
				// TODO: handle exception
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
			}
			break;
		}
	}

}
